package com.yyx.app1.MedicationAssistant_function.Controls.MedicationAssistant_Activity_Control.TreeListView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;

import com.yyx.app1.Data.DataInit;
import com.yyx.app1.MedicationAssistant_function.Controls.MedicationAssistant_Activity_Control.Three_ListView.ItemDataType;
import com.yyx.app1.MedicationAssistant_function.Controls.MedicationAssistant_Activity_Control.Three_ListView.ThreeListView_ItemView;
import com.yyx.app1.MedicationAssistant_function.Controls.MedicationAssistant_Activity_Control.Three_ListView.Three_List_Adapter;
import com.yyx.app1.MedicationAssistant_function.UI.MedicationAssistant_Activity;
import com.yyx.app1.MedicationAssistant_function.UI.MedicationInfo_Activity;
import com.yyx.app1.MedicationAssistant_function.UI.MedicationInfo_Activity_;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuexing on 2015/8/31.
 */
public class First_Second_ListViewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Map<String,String>> groupData;
    private List<List<Map<String, String>>> childData;
    private DataInit dataInit;

    public First_Second_ListViewAdapter(Context context) {
        this.context = context;
        dataInit=new DataInit(context,false,"cxlin");
    }

    public void findAll()
    {
        groupData = dataInit.getMedicationFirstList_SQLiteOperation().getAll();
        childData = new ArrayList<>();
        for (int i = 0 ; i < groupData.size() ; ++i)
        {
            String id = groupData.get(i).get("id");
            childData.add(dataInit.getMedicationSecondList_SQLiteOperation().getSecondByFirstId(id));
        }
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition).get("fcname");
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).get(childPosition).get("scname");
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        First_ItemView it;
        if (convertView == null)
        {
            it = First_ItemView_.build(context);
        }
        else
        {
            it= (First_ItemView)convertView;
        }
//        //判断实例可以展开，如果可以则改变右侧的图标
        if(isExpanded)
        {
            it.expand();
        }
        else
        {
            it.furl();
        }
        ItemDataType itemDataType = new ItemDataType(groupPosition,groupData.get(groupPosition).get("fcname"));
        it.bind(itemDataType);
        return it;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Second_ItemView it;
        if (convertView==null)
        {
            it= Second_ItemView_.build(context);
        }
        else
        {
            it = (Second_ItemView)convertView;
        }
        it.bind(new ItemDataType('A'+childPosition,childData.get(groupPosition).get(childPosition).get("scname")));
        return it;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        ListView listview_three = ((MedicationAssistant_Activity)context).getListview_three();
        Three_List_Adapter adapter = new Three_List_Adapter(context);
        adapter.findAll(childData.get(groupPosition).get(childPosition).get("id"));
        listview_three.setAdapter(adapter);
        listview_three.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, MedicationInfo_Activity_.class);
                String str = ((ThreeListView_ItemView) view).getLeft_listview_text_textview().getText().toString();
                intent.putExtra("medication", str);
                if (str != null) {
                    if (str.equals("暂无数据")) {

                    } else {
                        context.startActivity(intent);
                    }
                }
            }
        });
        return true;
    }
}
